var site_id = 15;
var product_finder_category_id = 2;
var selected_banner = 0;
var anz_banner;
var banner_animation;
var screenSize = screen.width + "x" + screen.height;
var imageurl = window.location.protocol + '//cnt.pentax.eu/CT/?campaign=screenSize&site=' + site_id + '&scrSz=' + screenSize + '&js=on';

var PE_counterEnd = new Date(2009, 4, 20, 16, 0, 0);
var PE_counterInt = null;

$(document).ready(
	function() {

/**
 * Show IE Version Warning
 */

		// alert(outputObject(jQuery.browser));

		if (jQuery.browser.msie
		&&  jQuery.browser.version < '7.0'
		&&  $.cookie('ie_version_warning') != 'off'	)
		{
			$('#cb_ie').click(function(){
				$.cookie('ie_version_warning', 'off', {path: '/'});
				$.fn.colorbox.close()
			});

			$.fn.colorbox({href: '#PE_IEVersionWarning', inline: true, open:true, width: 640});
		}

		/***
		 * Search Form
		 */
		if ( $('form#PE_bannerSearchForm input.PE_text_search').length >= 1 )
		{
			var default_search_string = $('form#PE_bannerSearchForm input.PE_text_search').val();
			$('form#PE_bannerSearchForm input.PE_text_search').focus(
				function()
				{
					if ($(this).val() == default_search_string) $(this).val('');
				}
			).blur(
				function()
				{
					if ($(this).val() == '') $(this).val(default_search_string);
				}
			)
		}

		// ColorBox init
		if ($('a.cbox_image').length > 0) {
			$('a.cbox_image').colorbox();
		}

		// BO Gallery-Cycle
		try {
			for ( i in banner ) {
				$('#PE_banner').append(
					'<img src="'+banner[i]+'" />'
				);
			}
		}
		catch(e) {}

		$('#PE_banner').cycle({
		    //fx:      'scrollRight',
		    fx:      'fade',
		    speed:    1000,
		    timeout:  5000,
				pause: 		1,
				next: 		'#PE_banner'
		});
		// EO Gallery-Cycle

		$('#SLR_demoInteractiv').cycle({
		    //fx:      'scrollRight',
		    fx:      'fade',
		    speed:    1000,
		    timeout:  5000,
				pause: 		1,
				next: 		'#SLR_demoInteractiv'
		});

/**
		 * Run Count-Down for new SLR
		 */
		try {
			if ($('#PE_counter').length > 0)
			{
				PE_counterStart();
			}
		}
		catch(e) {}

		/***
		 * Open Link-Target in Colorbox iFrame layer
		 * Anchors with this class must contain parameter width and height
		 */
		try {
			if ($('a.cbox_flexibleIframe').length > 0)
			{
				$('a.cbox_flexibleIframe').colorbox({
					innerWidth: function() {
						var href = $(this).attr('href').split('?');
						var query = href[1];
						var paramPairs = query.split('&');
						for (pair in paramPairs)
						{
							keyValue = paramPairs[pair].split('=');
							if (keyValue[0] == 'width')
							{
								return keyValue[1]
							}
						}
					},
					innerHeight: function() {
						var href = $(this).attr('href').split('?');
						var query = href[1];
						var paramPairs = query.split('&');
						for (pair in paramPairs)
						{
							keyValue = paramPairs[pair].split('=');
							if (keyValue[0] == 'height')
							{
								return keyValue[1]
							}
						}
					},
			  	iframe: true
			  });
			}
	  }
	  catch(e)
	  {
	  }

		/**
		 * Kamera-Finder
		 */
		if ( $('#PE_productFinder').length > 0)
		{
			try
			{
				PHPSESSID = urlGetSessionID();
				$.getScript('/public/sites/site_15/js/jquery.autocomplete.js',
					function() {
						getterUrl = 'getDCMSAjaxPlugin.php?site_id='+site_id+'&plugin_name=dcms_product_is_view_se&ajax_switch=product_selector&root_product_category_id='+product_finder_category_id+'&switch_lang='+page_lang;
						if ( PHPSESSID != '' )
						{
							getterUrl = getterUrl + '&PHPSESSID='+PHPSESSID;
						}
			  		$.getJSON(
			  			getterUrl,'',
			  			function(data)
			  			{
								product_url = data.product_url;
			  				category_array = data.category_array;
								$('input#PE_typeProductName').autocomplete(
									{ get : getMatches,
										callback: gotoProduct,
										multi: false,
										minchars: 0,
										timeout: 3000,
										delay: 100
									}
								);
			  			}
			  		);
					}
				);

				$('input#PE_typeProductName').focus(function() {
					txt_camera_name = $(this).val();
					$(this).val('');
				});
				$('input#PE_typeProductName').blur(function() {
					$(this).val(txt_camera_name);
				});

			} catch(e) {}
		}
	}
);

/**
 * Extract the Session-Id from the URL-Get-Parameters
 */
function urlGetSessionID()
{
	sessionID = '';
	paramPair_array = window.location.search.substring(1).split('&');
	jQuery.each(paramPair_array, function(idx, val) {
		paramVal_array = val.split('=');
		if (paramVal_array[0] == 'PHPSESSID')
		{
			sessionID = paramVal_array[1];
			return false;
		}
	});
	return sessionID;
}

function gotoProduct(obj) {
	$('input#PE_typeProductName').blur();
	window.location.href = obj.extra;
}

// *** Kamera-Finder:
function getMatches(typed)
{
	var a = Array();
	jQuery.each(category_array, function(idx, cat) {
		product_array = cat.product_array;
		jQuery.each(product_array, function(idx, val) {
			product_name = val.product_name
			if (product_name.match(new RegExp(typed, "i")))
			{
				new_href = cat.category_url.replace(/__group_id__/g, cat.category_id);
				new_href = new_href.replace(/__product_id__/g, val.product_nr);

	   		a.push({id:val.product_id, value: val.product_name, info:"<span class=\"\">"+cat.category_name+"</span>", extra: new_href});
			}

		});
	});
   return a;
}

// *** Kamera-Finder: Alle Kameras holen
function getAll()
{
	var a = Array();
	jQuery.each(category_array, function(idx, cat) {
		product_array = cat.product_array;
		jQuery.each(product_array, function(idx, val) {
			product_name = val.product_name;
   		a.push({id:val.product_id, value: val.product_name, info:"<a href=\""+new_href+"\">mehr &raquo;</a><br /><span class=\"\">"+cat.category_name+"</span><br /><span class=\"\">Artikelnr. "+val.product_nr+"</span>", extra: new_href});
		});
	});
   return a;
}


function PE_changeBanner()
{
	selected_banner++;
	if (selected_banner > anz_banner - 1 ) selected_banner = 0;
	// Preload des Banners
	var tmpImg = new Image();
	tmpImg.src = banner[selected_banner];
	// Ueberblenden
	$('#PE_bannerImg').fadeOut('slow',function() {
		$(this).attr('src',banner[selected_banner]);
		$(this).fadeIn('slow');
	});
}

/* Just for Debugging
*/
function outputObject(o) {
	var ret = '';
	for (var key in o)
	{
		ret = ret + key + ' = ' + o[key] + '\n';
	}
	return ret;
}

/**
* open window with some parameters
*/

	function wopen(link,wname,xsize,ysize,xpos,ypos,scrollbars,return_false)
	{
		if (scrollbars == '')
		{
			scrollbars = 'no';
		}

		if (xsize==-1)
			xsize=screen.availWidth-10;
		if (ysize==-1)
			ysize=screen.availHeight-60;
		if (xpos==-1)
			xpos=(screen.availWidth-xsize)/2;
		if (ypos==-1)
			ypos=(screen.availHeight-ysize)/2;

   	features = 'width='+xsize+',height='+ysize+',status=no,scrollbars='+scrollbars+',noresizable,top='+ypos+',left='+xpos+',screenY='+ypos+',screenX='+xpos;

		nwindow  = open(link, wname, features);
		nwindow.focus();

		if (return_false)
		{
			return false;
		}
	}


function PE_counterStart()
{
	PE_counterRefresh();
	PE_counterInt = setInterval('PE_counterRefresh()', 1000)
	$('#PE_counter').fadeIn();
}

function PE_counterStop()
{
	clearInterval(PE_counterInt);
}

function PE_counterRefresh()
{
	var PE_counterNow = new Date();
	PE_counterRest = Math.floor((PE_counterEnd - PE_counterNow) / 1000);
	if (PE_counterRest <= 0)
		PE_counterStop();

	days = Math.floor(PE_counterRest / 86400);
	$('#DD').text(days);
	PE_counterRest -= days * 86400;

	hours = Math.floor(PE_counterRest / 3600);
	$('#HH').text(hours);
	PE_counterRest -= hours * 3600;

	minutes = Math.floor(PE_counterRest / 60);
	$('#MM').text(minutes);
	PE_counterRest -= minutes * 60;

	$('#SS').text(PE_counterRest);
}

