// for the overview gallery
function deselect_text () {
  if (document.selection)
    document.selection.empty();
  else if (window.getSelection)
    window.getSelection().removeAllRanges();
}

if (itsAnOverviewPage) {
	document.write( '<style> #eventform { display:none; } </style>' );
	var kr_set = 0; //curent set
	var kr_sets = 1; //number of sets
	var kr_holdup = false; //see code below - limits clicks while doing a wrap scroll
}
if (itsAnImagePage) {
	document.write( '<style> #central_image_gal { overflow:hidden; height:0px; } #kr_col1 { min-height:360px; } </style>' );
}

var kr_anchors = new Array();

// colors
var kr_colors=new Array();
kr_colors[0]="#cfcfcf"; //off white -- other grey #7f7f7f
kr_colors[1]="#FF0000";
kr_colors[2]="#fff218";
kr_colors[3]="#00ff33";
kr_colors[4]="#00ccff";
kr_colors[5]="#ff66cc"; 
kr_colors[6]="#904b00";
kr_colors[7]="#dd7a10";

function rand( min, max ) {
    var argc = arguments.length;
    if (argc == 0) {
        min = 0;
        max = 2147483647;
    } else if (argc == 1) {
        throw new Error('Warning: rand() expects exactly 2 parameters, 1 given');
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
}
$(document).ready( function(){
	/*
	if ( !(document.referrer == location) ) {
		$('#kr_footer, #kr_body').hide().fadeIn();
	}
	*/
	
	
	if (itsTheHomePage) {
		$(window).resize(function(){
			var kr_feature_margin_top = 117; //added one pixel june 04- added another june 8
			var kr_footer_height = 43; //added december 17, for the new contact form footer
			var kr_feature_height = $(window).height() - kr_feature_margin_top - kr_footer_height;
			$('#kr_feature').css({
				 'position':'absolute','top': kr_feature_margin_top,'left':0,'overflow':'hidden',
				 'width':'100%', 'height':kr_feature_height })
			$('#kr_player')
				.attr( 'width','100%' )
				.attr( 'height',kr_feature_height )
		});
		$(window).resize();
		
		//new footer form
		$('#kr_subscribe').click( function() {
			$('#kr_subscribe_form_front_page').attr('action','/subscribehtml.php');
			if ( $('#kr_contact_email').attr('value').search('@') >= 0 ) $('#kr_subscribe_form_front_page').submit();
			else $('#kr_subscribe_error_message').hide().html('Enter a valid email address.').fadeIn('slow');
		})
	}
	
	/* if there are two links, on click of a link - only fade the bottom one. */
	if ($('.kr_nav_selected').size() >= 1) var fadoutselector = '#kr_col0 .kr_nav_selected'; 
	else var fadoutselector = '.kr_nav_selected';
	
	var kr_navDivs = '#kr_nav li a, #kr_col0 ul li a, #kr_col0 ul li ul li a, #page_wrap #sidebar ul a';
	if (itsTheMainNewGalleryPage) {
		kr_navDivs = kr_navDivs+', #kr_col1 a';
	}
	$(kr_navDivs).each( function (i) {
		if ($(this).hasClass("kr_nav_selected")) {
			$(this).css({color:kr_colors[i%7+1],cursor:'default'});
		} else {
			$(this).hover(
				function() { $(this).stop().css( {color:kr_colors[i%7+1]} ); },
				function() {
					$(this).animate(
						{color: kr_colors[0] },
						rand(300,800), 'swing',
						function(){ $(this).css({color:kr_colors[0]})
					});
				}
			);
			$(this).click( function() {
				$(this).stop().css({color:kr_colors[i%7+1],cursor:'pointer'}).hover( function(){}, function(){} )
				//if click on a sub nav link, don't fade out the header link
				if ( $(this).closest('div').get(0).id == 'kr_col0') var fadoutselector = '#kr_col0 .kr_nav_selected'; 
				else var fadoutselector = '.kr_nav_selected';				
				$(fadoutselector).animate(
					{color: kr_colors[0]},
					100, 'swing',
					function(){ $(this).css({color:kr_colors[0],cursor:'pointer'})
				});
				
				/*
				if (i !== 5) {
					//if it is not the webshop - which should open in a new window for the new cart
				*/
					if ( fadoutselector == '#kr_col0 .kr_nav_selected' ) $('#kr_footer, #kr_col1, #kr_col2').fadeOut(100);
					else $('#kr_footer, #kr_body').fadeOut(100);				
				/*
				}
				*/
				
				deselect_text ();
				
			});
		}
	})
	/* add color to links in column 1 */
	if (!itsASpecialPage) {
		$('.overview_link_group').remove();
		$('#kr_col1 a').each( function (i) {
			if ( this.href ) { //normal link
				$(this).css({ color : kr_colors[i%7+1] });
				if (itsTheMainNewGalleryPage) {
					//for new gallery
					$(this).css({ color : '#cfcfcf' });
				}
			} else if ( this.name ) { //anchor link
				$(this).css({ color : 'white' });
				kr_anchors[kr_anchors.length] = '<a href="#'+this.name+'">'+$(this).text()+'</a>';
			}
		})
		if (kr_anchors.length) {
			$('#kr_col1').prepend('<p id="kr_links_to_page_anchors"></p>');
			$.each(kr_anchors, function(i,v) {
				$('#kr_links_to_page_anchors').append(v+(i+1<kr_anchors.length?'<br />':''));
			});

			$('#kr_links_to_page_anchors a').each( function(i) {
				$(this).css({ color : '#7f7f7f' });
				$(this).hover(
					function() { $(this).stop().css( {color:kr_colors[i%7+1]} ); },
					function() {
						$(this).animate(
							{color: kr_colors[0] },
							rand(300,800), 'swing',
							function(){ $(this).css({color:kr_colors[0]})
						});
					}
				);
			})
			
		}
	}
	// RSS
	if (itsARSSPage) {
		$('#kr_col1 a').css({ color : kr_colors[0] }).each( function(i) {
			if (this.href == location) {
				$(this).css({color:kr_colors[i%7+1],cursor:'default'});
			} else {
				$(this).hover(
					function() { $(this).stop().css( {color:kr_colors[i%7+1]} ); },
					function() {
						$(this).animate(
							{color: kr_colors[0] },
							rand(300,800), 'swing',
							function(){ $(this).css({color:kr_colors[0]})
						});
					}
				);
			}
		})
		$('#kr_col2 img').each( function(i) {
			var colw = parseInt($('#kr_col2').css('width')) //587
			var w = $(this).attr('width')
			var h = $(this).attr('height')
			if ( w && h && w > colw/2 ) {
				neww = colw;
				newh = Math.round(h*(neww/w));
				$(this).css({width:neww}).attr('width',neww);
				$(this).css({height:newh}).attr('height',newh);
			}
		});
	//CONTACT PAGE
	} else if (itsTheMainContactPage) {
		$('#kr_subscribe, #kr_subscribe_form').click( function() {
			if (this.id == 'kr_subscribe') $('#kr_subscribe_form').attr('action','/subscribehtml.php');
			else if (this.id == 'kr_unsubscribe') $('#kr_subscribe_form').attr('action','/unsubscribehtml.php');
			if ( $('#kr_contact_email').attr('value').search('@') >= 0 ) $('#kr_subscribe_form').submit();
		})
		$('#kr_credit a').css({color:'#7F7F7F'});
		$('#kr_credit a').each( function(i) {
			$(this).hover(
				function() { $(this).stop().css( {color:kr_colors[rand(1,7)]} ); },
				function() {
					$(this).animate(
						{color:'#7F7F7F'},
						rand(300,800), 'swing',
						function(){ $(this).css({color:'#7F7F7F'})
					});
				}
			);
		})
	}
	
	//fix col2 and [OLD, removed: img gal portrait images]
	$('#kr_col2 img').each( function() {
	//	OLD $('#kr_col2 img, #central_image_gal img').each( function() {
		if ( $(this).width() / $(this).height() < 1 && $(this).width() <= 300 ) {
			$(this).css( {'margin-left':109} ); 
		};
	});
	
	// if it's an overview page, make the sliding image gallery
	if (itsAnOverviewPage) {		
		// Create the sliding gallery with an inside and outside div
		var inside_gal = document.createElement("div");
		var outside_gal = document.createElement("div");
		var number_of_blocks = $('.overview_block').size()
		kr_sets = Math.ceil(number_of_blocks/3);
		inside_gal.id = 'kr_inside_gal';
		outside_gal.id = 'kr_outside_gal';
		$('.overview_block').css({'margin-right':20,'margin-bottom':20}).wrapAll(inside_gal);
		$('#kr_inside_gal').wrapAll(outside_gal).css({
			'width': number_of_blocks*(272+20)-20+Math.floor($('.overview_block').size()/3) ,
			'min-height':223,
			'overflow':'hidden'
		});
		
		//Remove right margin from the last block
		$('.overview_block:last-child').css({'margin-right':0});
		// Add the one remaining pixel for the uneven layout: 272 | 20 | 272 | 21 | 272 (272 includes border).
		$('.overview_block').each( function(i) {
			if( (i+1)%3==0 ) $(this).css({'margin-left':1});
		})		
		
		/* FIXED WITH CSS
		//Fix the border on verticle image containers
		$('.overview_block .overview_image img').each( function() {
			//alert( $(this).width() );
			if ( this.width / this.height < 1 ) {
				$(this).closest('.overview_image').css({'width':this.width}); // margin left and right are auto 
			};
		});
		*/
		
		//Add the controls
		if (kr_sets > 1) {
			$('#kr_outside_gal').after( '<div id="kr_overview_nav"><span id="kr_prev">&lt;</span> &nbsp; <span id="kr_next">&gt;</span></p></div>' );
			$('#kr_prev').css({color:'#7f7f7f',cursor:'default'});
			$('#kr_next, #kr_prev').click( function() {
				// in case the user pressed the tab key...
				var scroll_left_from_tab = $('#kr_outside_gal').scrollLeft() ;
				if ( scroll_left_from_tab > 0) {
					kr_set = Math.floor(scroll_left_from_tab/877);
					$('#kr_outside_gal').scrollLeft(0);
				}

				if ( !(kr_holdup) ) {

					if ( this.id=='kr_next' && kr_sets == kr_set+1) {
						//end of the line
						kr_holdup = true;
						$('#kr_inside_gal').stop().animate( { marginLeft:(-877*(kr_set+1))+'px', opacity:0 }, 'fast', 'swing',
							function() { $(this).css( { marginLeft:(877)+'px' } ).animate( { marginLeft:(0)+'px', opacity:1}, 'fast', 'swing', function() { kr_holdup  = false } )
						} );
						kr_set_new = 0;
						kr_set = kr_set_new;
					} else if ( this.id=='kr_prev' && kr_set == 0) {
						//start of the line
						kr_holdup = true;
						$('#kr_inside_gal').stop().animate( { marginLeft:(877)+'px', opacity:0 }, 'fast', 'swing',
							function() { $(this).css( { marginLeft:(-877*(kr_sets))+'px' } ).animate( { marginLeft:(0)+(-877*(kr_sets-1))+'px', opacity:1}, 'fast', 'swing', function() { kr_holdup  = false } )
						} );
						kr_set_new = kr_sets-1;
						kr_set = kr_set_new;
					} else {
						//for regular scrolling
						if ( this.id=='kr_next' && kr_sets > kr_set+1) kr_set_new = kr_set+1;
						else if ( this.id=='kr_prev' && 0 <= kr_set-1) kr_set_new = kr_set-1			
						if (kr_set_new !== kr_set) {
							kr_set = kr_set_new;
							$('#kr_inside_gal').stop().animate( { marginLeft:(-877*kr_set)+'px', opacity:.6 }, 'normal', 'swing', function() { $(this).animate( {opacity:1}, 'fast' ) } );
							
							//removed this now that the scrolling products wrap
							//if ( (this.id=='kr_next' && !(kr_sets > kr_set+1) ) || (this.id=='kr_prev' && !(0 <= kr_set-1) ) ) {
							//	$(this).css({color:'#7f7f7f',cursor:'default'});
							//}
						}	
					}
				}
				
				deselect_text ();
			})
			$('#kr_next, #kr_prev').hover(
				function() {
					
					//replaced this
					//
					//if ( (this.id=='kr_next' && kr_sets > kr_set+1) || (this.id=='kr_prev' && 0 <= kr_set-1) ) {
					//	$(this).stop().css({color:'white',cursor:'pointer'});
					//}
					//
					//with this, now that the scrolling products "wraps:"
					$(this).stop().css({color:'white',cursor:'pointer'});
					
				},
				function() {
					$(this).stop().animate({color:'#7f7f7f'});
				}
			);
			$(document).keydown(function(e){
	 			// see http://ejohn.org/blog/keypress-in-safari-31/ on why keypress is bad
				c = e.which;
				//188 is the comma and the less than sign. 37 is the left arrow.
				//190 is the period and the gt sign. 39 is the right arrow.
				if (c == 188 || c == 37) $('#kr_prev').hover().click();
				if (c == 190 || c == 39) $('#kr_next').hover().click();
			});
		}
		
		//Scroll to the last product page you were viewing...
		if (krLastPage) {
			$('.overview_image img').closest('a').each( function(i) {
				var h = $(this).attr('href');
				var last_page_num = h.substr(h.length-krLastPage.toString().length, h.length);
				if ( parseInt(last_page_num) == parseInt(krLastPage) ) {
					kr_set = Math.floor(i/3);
					$('#kr_inside_gal').css( { marginLeft:(-877*kr_set) } );
					if (kr_set+1 == kr_sets) $('#kr_next').css({cursor:'default'});
				}
			})
		}
		
		$('#eventform').fadeIn();
	}
	
	//IMAGE GALLERY PAGE TYPE / PRODUCT PAGE
	if (itsAnImagePage) {
		//HOME PAGE GALLERY - DIFFERENT THAN OTHERS
		if (itsTheHomePage) {			
			if ($('#kr_homepage_slideshow img').size() > 1){

				var krHomepageSlideInterval;
				var krHomepageSlideMidSwitch = false;
	/*			
				$('#mosh_largeim_slideshow_container > img:first').bind("load", function(i){
					$('#mosh_largeim_slideshow_container > img:first').hide();  //necessary for chrome... dont know why
					$('#mosh_largeim_slideshow_container > img:first').fadeIn();
					if ($('#mosh_largeim_slideshow_container').children().length > 1){	
						$('#mosh_largeim_slideshow_container').click(function(){
							if ($($('#mosh_largeim_slideshow_container').children()[1]).attr('complete') && !moshHomepageSlideMidSwitch){
								moshHomepageSlideMidSwitch = true;
								$($('#mosh_largeim_slideshow_container').children()[1]).fadeIn(1000, function(){
									($($('#mosh_largeim_slideshow_container').children()[0])).hide();
									$('#mosh_largeim_slideshow_container').append($($('#mosh_largeim_slideshow_container').children()[0]));
									moshHomepageSlideMidSwitch = false;
								});
								clearInterval(moshHomepageSlideInterval);
								moshHomepageSlideInterval = setInterval("$('#mosh_largeim_slideshow_container').click()", 5000);
							}
						});
						moshHomepageSlideInterval = setInterval("$('#mosh_largeim_slideshow_container').click()", 5000);
					}
				});
*/		
				$('#kr_homepage_slideshow img').each( function(i) {
					$(this).hide();
					$(this).click( function() { 
						var next = $(this).next();
						if (next.size() == 0) {
							next = $('#kr_homepage_slideshow img:first-child');
							i=-1;
						}
						if (next.attr('complete') && !krHomepageSlideMidSwitch){
							krHomepageSlideMidSwitch = true;
							$(this).stop().fadeOut(1000);
							next.stop().fadeIn(1000, function(){
								krHomepageSlideMidSwitch = false;
							});
							clearInterval(krHomepageSlideInterval);
							krHomepageSlideInterval = setInterval("$('#kr_homepage_slideshow img:eq("+(i+1)+")').click()", 5000);
						}
					});
				});
				$('#kr_homepage_slideshow img:first-child').fadeIn(1000);
				krHomepageSlideInterval = setInterval("$('#kr_homepage_slideshow img:eq("+0+")').click()", 5000);
			}
		} else {
			// GALLERY FOR PRODUCTS AND WEBSHOP PAGES
			$('#central_image_gal div').hide().css({'position':'absolute'});
			$('#central_image_gal div:last').after('<span id="kr_gal_control"></span>');
			$('#central_image_gal div').each( function(i) {
				$('#kr_gal_control').append(' <span class="kr_gal_control_number">&nbsp;'+(i+1)+'&nbsp;</span>');
				$(this).click( function() { 
					if ( (i+1) == $('#central_image_gal div').size() ) {
						$('.kr_gal_control_number:nth-child('+1+')').click();
					} else {
						$('.kr_gal_control_number:nth-child('+((i+1)+1)+')').click();
					}
				})
			});
			$('.kr_gal_control_number').each( function(i) {
				$(this).click( function() {
					$('#central_image_gal div').stop().fadeOut( function(){ $(this).css({'display':'none','opacity':1}) } );
					$('#central_image_gal div:eq('+i+')').stop().fadeIn( function(){ $(this).css({'opacity':1}) } );
					$('.kr_gal_control_number').css({color:'#7f7f7f'});
					$(this).css({color:'white'});
				})
			});
			$('.kr_gal_control_number:first-child').click();
			$('#central_image_gal').css( {'overflow':'visible','height':'auto'} );
		}
	}
	if ($('#kr_back_link').size()>0){
		if ($('#kr_gal_control').size()>0){
			if ($('#kr_back_link').offset().top < $('.kr_gal_control_number').offset().top) { 
				$('#kr_back_link').css('position','absolute');
				$('#kr_back_link').css('top',$('.kr_gal_control_number').offset().top - $('.kr_gal_control_number').height());
			}
		} else if ($('#kr_layout_image_2').size()>0){
			var margtop = $('#kr_layout_image_2').offset().top + $('#kr_layout_image_2').height() - $('#kr_back_link').offset().top;
			if (margtop > 0) $('#kr_back_link').css('margin-top', margtop+'px');
		}
	}
	
})

//go home when ESC is pressed
$(document).keyup(function(e) {
	c = e.which;
	if (c == 27) { setTimeout(' window.location = "/" ',10); }
});