var selected_banner = 0;
var anz_banner;
var banner_animation;

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

$(document).ready(
	function() {

		if ( $('#PE_page_1006, #PE_page_998').length >= 1 && $.browser.msie == true && $.browser.version == "6.0")
		{
			$.getScript('/site/js/jquery.pngfix.js',
				function() {
					$('#pngFix, #pngFix1').pngfix();
				}
			);
		}

		// Colorbox for SSD-Product-Images
		if ( $('a.cbox_image').length > 0)
		{
			$('a.cbox_image').colorbox();
		}

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

		$('#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'
		});

		// *** Toggle Answers of FAQ-Section
		$('#PE_soFaq h3.PE_faqQuestion').toggle(
			function() {
				$('#PE_soFaq div.PE_faqAnswer').hide();
				next_siblings = $(this).next();
				next_elem = next_siblings[0];
				$(next_elem).show();
			},
			function() {
				next_siblings = $(this).next();
				next_elem = next_siblings[0];
				$(next_elem).hide();
			}
		);

		try {
			$('#PE_soSchemeBino div.PE_soSensi').click(
				function()
				{
					$('#PE_soSchemeBino .PE_contentBox:visible').hide();
					id_array = $(this).attr('id').split('_');
					id = 'dot_' + id_array[1];
					$('#'+id).fadeIn('slow');
				}
			);
		}
		catch(e) {}

		try {
			$('#PE_soSchemeSpek div.PE_soSensi').click(
				function()
				{
					$('#PE_soSchemeSpek .PE_contentBox:visible').hide();
					id_array = $(this).attr('id').split('_');
					id = 'dot_' + id_array[1];
					$('#'+id).fadeIn('slow');
				}
			);
		}
		catch(e) {}

		try {
			//alert($('#PE_product_award_ticker ul').children().length);
			if ( $('#PE_product_award_ticker ul').children().length > 6 )
			{
				$('#PE_product_award_ticker').jCarouselLite({
			    auto: 2000,
			    speed: 1000,
			    visible: 6
		   	});
			}
    }
    catch(e)
    {
    }

		/**
		 * 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');
						m = href.match(/width[\/=]([0-9]+)/);
						return m[1];
					},
					innerHeight: function() {
						var href = $(this).attr('href');
						m = href.match(/height[\/=]([0-9]+)/);
						return m[1];
					},
			  	iframe: true
			  });
			}
	  }
	  catch(e)
	  {
	  }

		// initClickCount('a[href]');

	}
);



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;
		}
	}

/**
* schickt ein Formular an die Thickbox, wo es im IFRAME geöffnet wird
*/

function tb_sendform(f,c)
{
  f.action.match(/(\bkeepThis=(true|false)&TB_iframe=true.+$)/);
  tb_show(c, 'about:blank?'+RegExp.$1);
  f.target=$('#TB_iframeContent').attr('name')
  return true;
}

function initClickCount(selector)
{
	if ($(selector).size() > 0)
	{
		$(selector).each(function()
		{
			absUrl = false;
			container = $(this).parents('[@id]').attr('id');
			href = $(this).attr('href');

			if (ergebnis = href.match(/^http:\/\//i))
			{
				absUrl = true;
			}
			else if (ergebnis = href.match(/^https:\/\//i))
			{
				absUrl = true;
			}

			if (absUrl)
			{
				newHref = 'http://cnt.pentax.de/RD/?campaign=clicks&container=' + container + '&callback=' + urlencode(href);
				$(this).attr('href', newHref);
			}
		});
	}
}

function urlencode( str ) {

    var histogram = {}, tmp_arr = [];
    var ret = str.toString();

    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };

    // The histogram is identical to the one in urldecode.
    histogram["'"]   = '%27';
    histogram['(']   = '%28';
    histogram[')']   = '%29';
    histogram['*']   = '%2A';
    histogram['~']   = '%7E';
    histogram['!']   = '%21';
    histogram['%20'] = '+';

    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);

    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }

    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });

    return ret;
}

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);
}

