var hexcolor = "#000000";
var redcolor = 0;
var greencolor = 0;
var bluecolor = 0;

var clickcounter = 0;
//localStorage.chooseclickcounter = 0;

$(document).ready(function() {
	$("#vserv_advt_slide, #vserv_advt_slide_popup").click(function(event) {
		localStorage.chooseclickcounter = 0;
	});
});


$(document).ready(function() {
	var height = parseInt(($(document).innerHeight() * 2)/3);

	ad_height = parseInt($(document).innerHeight() * 0.6);

	$("#vserv_advt_slide li").each(function(){
		$(this).find("img").css("width","100%");
		$(this).find("img").css("height",height-50);
	});
	
	$("#vserv_advt_slide_popup li").each(function(){
		$(this).find("img").css("width","100%");
		$(this).find("img").css("height",height-50);
	});
	
	$("#clickbank_advt_popup li").each(function(){
		$(this).find("img").css("width","100%");
		$(this).find("img").css("height",ad_height);
	});
});

var advttimer = setInterval(function(){
	$.mobile.changePage('#popup_advt_page', {
		transition : 'pop',
			     role       : 'dialog'
	});
}, 120000);


function colorupdate()
{
    redcolor = document.getElementById("redslider").value;
    greencolor = document.getElementById("greenslider").value;
    bluecolor = document.getElementById("blueslider").value;
    
    var hexred = "";
    var hexgreen = "";
    var hexblue = "";
    
    if(redcolor < 15)
        hexred = "0" + Number(redcolor).toString(16);
    else
        hexred = Number(redcolor).toString(16);
        
    if(greencolor < 15)
        hexgreen = "0" + Number(greencolor).toString(16);
    else
        hexgreen = Number(greencolor).toString(16);
        
    if(bluecolor < 15)
        hexblue = "0" + Number(bluecolor).toString(16);
    else
        hexblue = Number(bluecolor).toString(16);
        

    hexcolor = "#" + hexred + hexgreen + hexblue;

    document.getElementById("displaycolor").style.backgroundColor = hexcolor;
    
}

$(document).ready(function() {
	$( '#calculate' ).click( function() {
		clickcounter = Number(localStorage.chooseclickcounter);
		
		
		if(isNaN(clickcounter))
			clickcounter = 0;

		if(clickcounter >= 5)
		{
			alert("Time to buy me a beer. \nKindly click and visit offers. \nYou will get 5 chances.\n(If advt. not shown...plz check your internet connection and reload apps.)");
			$.mobile.changePage("#advt_page", { transition: "slide", changeHash: true});
		}
		else
		{
			if(localStorage.chooseclickcounter)
				localStorage.chooseclickcounter = Number(localStorage.chooseclickcounter) + 1;
			else
				localStorage.chooseclickcounter = 1;

			if(((redcolor < 127) && (greencolor < 127)) || ((redcolor < 127) && (bluecolor < 127)) || ((greencolor < 127) && (bluecolor < 127)))
			{
				document.getElementById("hexbgcolor").style.color = "#ffffff";
			}
			else
			{
				document.getElementById("hexbgcolor").style.color = "#000000";
			}
			
			document.getElementById("hexbgcolor").style.backgroundColor = hexcolor;
			document.getElementById("redcell").innerHTML = redcolor;
			document.getElementById("greencell").innerHTML = greencolor;
			document.getElementById("bluecell").innerHTML = bluecolor;
			document.getElementById("hexvalue").innerHTML = hexcolor;
			
			/////////////////////////////
			var red = (redcolor)/255;
			var green = (greencolor)/255;
			var blue = (bluecolor)/255;
			
			var hue = 0;
			var saturation = 0;
			var hsl_saturation = 0;
			var value  = 0;
			
			var del_R = 0;
			var del_G = 0;
			var del_B = 0;

			var var_Max = Math.max(red, Math.max(green, blue));
			var var_Min = Math.min(red, Math.min(green, blue));
			var del_Max = var_Max - var_Min;

			var lightness = (var_Max + var_Min)/2;

			var cyan = 0;
			var magenta = 0;
			var yellow = 0;
			var key = 0;

			value = var_Max;

			if(del_Max == 0)
			{
			    hue = 0;
			    saturation = 0;
			}
			else
			{
			     if (lightness < 0.5)
			     {
				 hsl_saturation = del_Max/(var_Max + var_Min);
			     }
			     else
			     {
				 hsl_saturation = del_Max / ( 2 - var_Max - var_Min );
			     }

			    saturation = del_Max/var_Max;

			   del_R = ( ( ( var_Max - red ) / 6 ) + ( del_Max / 2 ) ) / del_Max;
			   del_G = ( ( ( var_Max - green ) / 6 ) + ( del_Max / 2 ) ) / del_Max;
			   del_B = ( ( ( var_Max - blue ) / 6 ) + ( del_Max / 2 ) ) / del_Max;

			       if(red == var_Max )
			       {
				   hue = del_B - del_G;
			       }
			       else if( green == var_Max )
			       {
				   var a = 1;
				   var b = 3;
				   hue = ( a / b ) + del_R - del_B;
			       }
			       else if(blue == var_Max)
			       {
				   var a = 2;
				   var b = 3;
				   hue = ( a / b ) + del_G - del_R;
			       }

			       if(hue < 0 )
				   hue = hue + 1;

			       else if (hue > 1 )
				   hue = hue - 1;

			}

			hue = hue * 360;
			saturation = saturation * 100;
			value = value * 100;

			hsl_saturation = hsl_saturation * 100;
			lightness = lightness * 100;

			if (redcolor == 0 && greencolor == 0 && bluecolor == 0)
			{
			    cyan = 0;
			    magenta = 0;
			    yellow = 0;
			    key = 1;
			}			
			else
			{
			    key = 1 - Math.max(red, Math.max(green, blue));
			    cyan = (1 - red - key)/(1 - key);
			    magenta = (1 - green - key) / (1 - key);
			    yellow = (1 - blue - key) / (1 - key);
			}

		    document.getElementById("huecell").innerHTML = Math.round(hue*100)/100;
		    document.getElementById("hslhuecell").innerHTML = Math.round(hue*100)/100;
		    document.getElementById("saturationcell").innerHTML = Math.round(saturation*100)/100;
		    document.getElementById("hslsaturationcell").innerHTML = Math.round(hsl_saturation*100)/100 + "%";
		    document.getElementById("valuecell").innerHTML = Math.round(value*100)/100;
		    document.getElementById("lightnesscell").innerHTML = Math.round(lightness*100)/100 + "%";
		    document.getElementById("cyancell").innerHTML = Math.round(cyan*100)/100;
		    document.getElementById("magentacell").innerHTML = Math.round(magenta*100)/100;
		    document.getElementById("yellowcell").innerHTML = Math.round(yellow*100)/100;
		    document.getElementById("keyblackcell").innerHTML = Math.round(key*100)/100;


	    /////////////////////////////        

		    $.mobile.changePage("#page_4", { transition: "slide", changeHash: true});
		}
	});
});


$(window).load(function(){
	$("#vserv_advt_slide").responsiveSlides({
		"auto": true,
		"pause": false,
		"pauseControls": false,
		"namespace": "rslides",
		"nav": true,
		"maxwidth": 300,
		"maxheight": 300,
		"prevText": "< ",   // String: Text for the "previous" button
		"nextText": " >",       // String: Text for the "next" button
	});	
});

$(window).load(function(){
	$("#vserv_advt_slide_popup, #clickbank_advt_popup").responsiveSlides({
		"auto": true,
		"pause": false,
		"pauseControls": false,
		"namespace": "rslides",
		"nav": true,
		"maxwidth": 300,
		"maxheight": 300,
		"prevText": "< ",   // String: Text for the "previous" button
		"nextText": " >",       // String: Text for the "next" button
	});	
});


$(window).load(function(){
	$("#clickbank_advt1").responsiveSlides({
		"auto": true,
		"pause": false,
		"pauseControls": false,
		"namespace": "rslides",
		"nav": true,
		"maxwidth": 300,
		"maxheight": 300,
		"prevText": "< ",   // String: Text for the "previous" button
		"nextText": " >",       // String: Text for the "next" button

	});
});

$(window).load(function(){
	$("#clickbank_advt2").responsiveSlides({
		"auto": true,
		"pause": false,
		"pauseControls": false,
		"namespace": "rslides",
		"nav": true,
		"maxwidth": 300,
		"maxheight": 300,
		"prevText": "< ",   // String: Text for the "previous" button
		"nextText": " >",       // String: Text for the "next" button

	});
});

