
// usage: log('inside coolFunc', this, arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  arguments.callee = arguments.callee.caller;  
  if(this.console) console.log( Array.prototype.slice.call(arguments) );
};
// make it safe to use console.log always
(function(b){function c(){}for(var d="assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(","),a;a=d.pop();)b[a]=b[a]||c})(window.console=window.console||{});


// place any jQuery/helper plugins in here, instead of separate, slower script files.





/** *************************************************
 *  Author: Jason Cook
 *  Website: Scriptjerk.com
 *  Email: greenscripts@gmail.com
 *  *************************************************
*/




// Modernizr Video
if (Modernizr.video) {
	// let's play some video! but what kind?
	if (Modernizr.video.webm) {
		// try WebM
	} else if (Modernizr.video.ogg) {
		// try Ogg Theora + Vorbis in an Ogg container
	} else if (Modernizr.video.h264){
		// try H.264 video + AAC audio in an MP4 container
	}
}






// Window Onload
$(document).ready(function() {  

	// Variables
	var posX = 0
	var posY = 0
	var numZ = 0
	var windowWidth = 0
	var windowHeight = 0	
	var hscroll = document.getElementById('body').scrollHeight
	var scrolldirection = 'down'
	var sticky = false

	// Elements
	var menu = document.getElementById('menu');	
	var docErr = document.getElementById( 'err' )
	var docMsg = document.getElementById( 'msg' )
	var over = document.getElementById('over');
	var wrap = document.getElementById('wrap');	
			
	// Debug Box
	var consl = document.getElementById('fixedconsole');

	// Window Dimensions
	if(document.body && document.body.offsetWidth){
		windowWidth = document.body.offsetWidth;
		windowHeight = document.body.offsetHeight;
	}
	if(document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth){
		windowWidth = document.documentElement.offsetWidth;
	 	windowHeight = document.documentElement.offsetHeight;
	}
	if(window.innerWidth && window.innerHeight){
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	}

	// Scroll Iterations
	var iteration = Math.round((hscroll - windowHeight) / 12)
	
	// Adjust position initially
	var ttd = parallaxMath(1000, 15, 0)
	//over.style.backgroundPosition = "-" + ttd + "px " + ttd + "px";
	
	
	// Init Lightbox
	$('#gallery a').lightBox();	
	
	$('ul#jcloud-tags').jcloud({
		radius:120,          //    cloud radius
		size:22,             //    tags font size
		step:4,              //    cloud step
		speed:70,            //    cloud speed
		flats:2,             //    flats count
		clock:10,            //    timer interval
		areal:100,           //    cloud areal
		splitX:60,          //    X-axis delta
		splitY:40,          //    Y-axis delta
		//colors:['#000000','#DD2222','#2267DD','#2A872B','#872A7B','#CAC641']
		colors:['#B528ED','#6F8BDE','#8D6AA8','#63B0D6','#DE62A2','#587DED']
	});	
	
	
	
	
	
	
	
	
	
	
	
	
	$('.top').addClass('hidden');
	$.waypoints.settings.scrollThrottle = 0;
	$('.body').waypoint(function(event, direction) {
		$('.top').toggleClass('hidden', direction === "up");
	}, {
		offset: '-100%'
	}).find('#menu').waypoint(function(event, direction) {
		$(this).parent().toggleClass('sticky', direction === "down");
		event.stopPropagation();
	});
	
	
	
	
	// Parallax Scroll Effect
	window.onscroll = function() {
		posX = (document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : window.pageXOffset;
		posY = (document.documentElement.scrollTop) ? document.documentElement.scrollTop : window.pageYOffset;
		
		
		if(numZ < posY){
			scrolldirection = 'down'
		}else{
			scrolldirection = 'up'
		}
		numZ = posY
		
		
		var parallaxOver = parallaxMath(1360, 15, posY);
		var parallaxWrap = parallaxMath(1200, 5, posY);	
		
			
		//over.style.backgroundPosition = "-" + parallaxOver + "px " + parallaxOver + "px"; 
		wrap.style.backgroundPosition = parallaxWrap + "px " + parallaxWrap + "px"; 
		
		// Debug Box
		if(consl)
		consl.innerHTML = posY + " / " + hscroll + ""


		if(posY > 83 && sticky == false){
			sticky = true;
			//$('nav').toggleClass('fixed')
			//$('nav').stop().animate({ position: "fixed"}, 0);	
			$('nav').toggleClass('sticky')
			//$('.navfirst').toggleClass('fnav')
			$('.minilogo').fadeIn('slow', function() {
				// Animation complete
			});			
		}

		if(posY < 82 && sticky == true){
			sticky = false;
			//$('nav').stop().animate({ position: "relative"}, 0);	
			//$('nav').toggleClass('fixed')
			$('nav').toggleClass('sticky')
			//$('.navfirst').toggleClass('fnav')
			$('.minilogo').fadeOut('fast', function() {
				// Animation complete
			});	
		}


		
		// Colorize effect :: Needs simplified
		if(posY > (iteration * 0) && posY < (iteration * 1))
			$('#body').stop().animate({ backgroundColor: "#B7B9E8"}, 1000);		
		if(posY > (iteration * 1) && posY < (iteration * 2))
			$('#body').stop().animate({ backgroundColor: "#7799CC"}, 1000);
		if(posY > (iteration * 2) && posY < (iteration * 3))
			$('#body').stop().animate({ backgroundColor: "#77AACC"}, 1000);	
		if(posY > (iteration * 3) && posY < (iteration * 4))
			$('#body').stop().animate({ backgroundColor: "#77CC99"}, 1000);	
		if(posY > (iteration * 4) && posY < (iteration * 5))
			$('#body').stop().animate({ backgroundColor: "#AACC77"}, 1000);
		if(posY > (iteration * 5) && posY < (iteration * 6))
			$('#body').stop().animate({ backgroundColor: "#CCCC77"}, 1000);
		if(posY > (iteration * 6) && posY < (iteration * 7))
			$('#body').stop().animate({ backgroundColor: "#CCBB77"}, 1000);
		if(posY > (iteration * 7) && posY < (iteration * 8))
			$('#body').stop().animate({ backgroundColor: "#CC8877"}, 1000);
		if(posY > (iteration * 8) && posY < (iteration * 9))
			$('#body').stop().animate({ backgroundColor: "#CC7799"}, 1000);
		if(posY > (iteration * 9) && posY < (iteration * 10))
			$('#body').stop().animate({ backgroundColor: "#CC77CC"}, 1000);
		if(posY > (iteration * 10) && posY < (iteration * 11))
			$('#body').stop().animate({ backgroundColor: "#AA77CC"}, 1000);
		if(posY > (iteration * 11) && posY < (iteration * 12))
			$('#body').stop().animate({ backgroundColor: "#6D6D99"}, 1000);
		
	}

	// Url text update :: Experimental
	$("a.ls").click(function(event) {
	  	event.preventDefault();
	  	if (typeof history.pushState === "function"){
			history.pushState('test', 'title', 'tester.html');
		}
	});
	
	

	// Remember Me Checkbox
	var remember
	var checkdoc
	$(".checkd").click(function(event) {
		remember = document.getElementById('remember')
	  	checkdoc = document.getElementById('checkt')
	  	if(remember.value == '0'){
	  		checkdoc.innerHTML = 'x'
	  		remember.value = '1'
	  	}else{
	  		checkdoc.innerHTML = ''
	  		remember.value = '0'
	  	}
	});	
	
	if(session_uid != '' && setcookie){
		js_cookie(logincookie,'');
	}
	
	if(deletecookie == 'true'){
		Unset_Cookie( 'ScriptJerk-Auto-Login', '', '/', '', '' )
		document.location = siteurl
	}

	// Add Promotion Code
	$("#promobutton").click(function(event) {
	  	var code = document.getElementById('promotion').value
	  	document.getElementById('sub_promotion').value = code
	  	document.promoupdate.submit();
	});
	

	// Add to cart buton
	$("div#addtocart").click(function(event) {
	  	document.tocart.submit();
	});		
	// Add to cart buton
	$("#crtlogin").click(function(event) {
	  	document.location = siteurl + '/login/return/cart';
	});
	
	$("div[id*='addtocart_']").click(function(event){
		//alert('this button');
		var rid = $(this).attr("rel");
		var mid = $(this).attr("id");
        var mdat = mid.split('_');
        var xid = mdat[1];
        document.getElementById('options').value = '1'
        document.getElementById('option_id').value = xid
        document.getElementById('option_price').value = rid
        document.tocart.submit();
        
	});			
		
	
    $("a[id*='xquan_']").click(function(event){
        var mid = $(this).attr("id");
        var mdat = mid.split('_');
        var xid = mdat[2];
		var typ = mdat[1];
		var num = document.getElementById('2' + mid).value
		// jQuery Ajax Post
		var url = siteurl + '/cart'
		$.post(url,{ 
			type : typ,
			xid : xid,
			qua : num,
			action : 'update'
			},function(output){
				document.location = url
			}
		);		
		
	});	
	
    $("a[id*='xdel_']").click(function(event){
        var mid = $(this).attr("id");
        var mdat = mid.split('_');
        var xid = mdat[2];
		var typ = mdat[1];
		var num = 0
		// jQuery Ajax Post
		var url = siteurl + '/cart'
		$.post(url,{ 
			type : typ,
			xid : xid,
			qua : num,
			action : 'update'
			},function(output){
				if(output == ' success'){
					document.location = url
				}else{
					alert('Failed to delete item!');
				}
			}
		);		
		
	});		
	

	// Item Images
	$("img[id*='imgx_']").click(function(event) {
	  	var imx = $(this).attr("src");
	  	docimg = document.getElementById('thisimg')
	  	docimg.src = imx
	  	//$('#gallery a').lightBox();	
	  	//initLightbox();
	  	//updateImageList()
	});		
	
	

});  

function downloadform(id){
	document.forms[id].submit();
}


// --- Standard Functions --------------------------------------------------------------------
 


// Parallax Height Scroll 
function parallaxMath(h, s, p) {
	var h1 = h + 1
	var re = Math.floor(p / s) % h1
  	return h-re;
}











function Unset_Cookie( name, value, path, domain, secure ){
	var expires = -3600;
	var today = new Date();
	today.setTime( today.getTime() );
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}


function Set_Cookie( name, value, path, domain, secure ){
	var expires = 1000 * 60 * 60 * 24 * 365;
	var today = new Date();
	today.setTime( today.getTime() );
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}


function Get_Cookie( check_name ) {
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false;
	for ( i = 0; i < a_all_cookies.length; i++ ){
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( cookie_name == check_name ){
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 ){
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ){
		return null;
	}
}

function js_cookie(data,module){
	if(Get_Cookie( 'ScriptJerk-Auto-Login' ) == null){
		Set_Cookie( 'ScriptJerk-Auto-Login', data, '/', '', '' )
		js_redirect(siteurl + '', false)
	}
}



function js_redirect(url,p){			
  if(p){
  	parent.document.location = url;
  } else {
  	document.location = url;
  }
}









