//. quick notes..


/***********************************************
* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

$(document).ready(function() {
	
	//. - disable text Selection.
	function disableSelection(target){
		if (typeof target.onselectstart!="undefined") //IE route
			target.onselectstart=function(){return false}
		else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
			target.style.MozUserSelect="none"
		else //All other route (ie: Opera)
			target.onmousedown=function(){return false}
		target.style.cursor = "default"
	}
	//. - Sample usages  -  Disable text selection
	//disableSelection(document.body);
	//disableSelection(document.getElementById("mydiv"));
	
	
	
	var xmlLocation = "banner/images_XML.xml";
        var folder = "banner/images/";
        var FileType = ".jpg";
	
    var timerBetweenimages = 15000;
    var whatBrowser = $.browser;
    var Playbtn = true;
    var isEnabled = true;
    var firstIMGnum = 0;
    var loopani;
    var whatIMG = 0;
    var height = 220;
    var nextIMGSpacing = height;
    var nextIMGSpacingCount = 0; //counts by 220 or height.
    var newIMG = '#ImgDivFirst'

    var settings = {
	tl: { radius: 8 },
	tr: { radius: 8 },
	bl: { radius: 8 },
	br: { radius: 8 },
	antiAlias: true
    }
		
    $('#btnH').css({ 'top': '220px'});
    $('#dropShadowid').css({ 'opacity': 0 });
    $('#boxText_btn').css({ 'opacity': 0 });
    $('#boxText_Head').css({ 'opacity': 1 });
    $('#boxText_Com').css({ 'opacity': 1 });
    
    function createIMG(){
	
    isEnabled = false;
    $('#btnPlay').unbind('click', btnPlayClick);
    //$('#btnPlay').prop("disabled", true);
	clearTimeout(loopani);
	
	//.Creates second div.
    	var nextDiv;
    	nextDiv = document.createElement("div");
    	nextDiv.setAttribute('id', 'nextIMG');
    	nextDiv.setAttribute('class', 'ImgStyle');
	$(nextDiv).css({ 'height': '220px', 'width': '930px'});
	
    	var grabCont = document.getElementById("ImgHolder");
    	grabCont.appendChild(nextDiv);
	
	$("#nextIMG").addClass("ImgStyle");
    
	$('#nextIMG').css({ 'top': nextIMGSpacing});
	nextIMGSpacing += height;
	
        var imgName = imageName[whatIMG];
        var FimgF = new Image();
	FimgF.src = folder+imgName+FileType;
	
	var StopWait = window.setTimeout(StopWaitFun, 300, "JavaScript");
	function StopWaitFun(){
	    clearTimeout(StopWait);
		
	    $('#nextIMG').css({ 'background': 'url('+ FimgF.src +')' });
	    
	    animate();
	}
    }

    function animate(){
	    speed = 800;
	    distEND = $("#imgDivHoder").height()+nextIMGSpacingCount;
	    nextIMGSpacingCount+=height;
	    
	    $('#boxText_Head').animate({ "top": -height },
		{ duration:speed, easing:"quadEaseInOut"} );
	    $('#boxText_Com').animate({ "top": -height },
		{ duration:speed, easing:"quadEaseInOut"} );
	    
	    $('#ImgHolder').animate({ "top": -distEND },
		{ duration:speed, easing:"quadEaseInOut" , complete: AnimateDone } );
	   
	function AnimateDone(){
	     FadeinTextimg();
	}
    }
    
    
    
// EaseInOut //
//- back 
//- bounce
//- circ
//- cubic
//- elastic
//- expo
//- quad
//- quart
//- quint
//- sine


    function disableBTN() {
	
	for(var i=0; i<imageName.length; i++){
	    $('#btn'+i).prop("disabled", true);
	}
    }
    
    function enableBTN() {
	for(var i=0; i<imageName.length; i++){
	    $('#btn'+i).prop("disabled", false);
	}
    }
    
    

	//. - Box text and boxBtn click for url or local location.
	var boxtextArray = [];
	boxtextArray[0] = '#boxText_Head';
	boxtextArray[1] = '#boxText_Com';

	$('#boxText_btn').click(function() {
	    //alert(imageLinks[whatIMG]);
	    
	    window.location.href = imageLinks[whatIMG];
	    //window.location.href = "http://www.adenviro.com/";
	});
	
	
	function FadeinTextimg(){
		//margin-top: 65px;
		//margin-left: 23px;
		$('#boxText_Head').css({ 'opacity': 0 });
		$('#boxText_Com').css({ 'opacity': 0 });
		
		var head = folder+imageNameHead[whatIMG]+FileType;
		var comm = folder+imageNameCom[whatIMG]+FileType;
	    $('#boxText_Head').css({ 'background-image': 'url('+ head +')'  });
	    $('#boxText_Com').css({ 'background-image': 'url('+ comm +')' });
		
	    //.Animate box text fadeIn.
		var wichBox = 0;
		aniBox();
		function aniBox(){
			$(boxtextArray[wichBox]).css({  'top': '0px'})
			$(boxtextArray[wichBox]).animate({ "opacity": 1 },
			    { duration:400, easing:"quadEaseInOut" , complete: AnimateDone } );
				function AnimateDone(){
					wichBox++
					if (wichBox<=1) { aniBox() }
					else {
					    
					wichBox = 0;
					
					$('#ImgDivFirst').detach();
					var getDiv = document.getElementById("nextIMG");
					getDiv.setAttribute('id', 'ImgDivFirst');
					   
					isEnabled = true;
					$('#btnPlay').bind('click', btnPlayClick);
					
					loopani = window.setInterval(loopaniFun, timerBetweenimages, "JavaScript");
					//loopaniFun();
					//alert("Done")
					}
				}
		} 
	}


    loopani = window.setInterval(loopaniFun, timerBetweenimages, "JavaScript");
    function loopaniFun(){

	if (Playbtn == true){
	    clearTimeout(loopani);
	    loop_Animation();
	}
    }
    
    function loop_Animation() {
	$('#objD').remove();
	whatIMG++;
	    if (whatIMG >= imageName.length ){
		whatIMG =0; 
	    }
	    createIMG();
	    btnSelChange_Ani();
    }
    
    function btnSelChange_Ani(){
	
	var selectedIndex = $('#btnH').children().size() -2;
	
	if ( grabIndex >= selectedIndex  ){
	  grabIndex = -1;  
	}
	grabIndex++;
	
	grabObj = document.getElementById("btn"+grabIndex);
	create_onClick_Diplay();
	
    }
    
    function create_onClick_Diplay(){
	    
	$('#objD').remove();
	
	var objDesign;
	    objDesign = document.createElement("div");
	    objDesign.setAttribute('id', 'objD');
	    objDesign.setAttribute('class', 'btnsel');
	    
	    $(objDesign).text(grabIndex+1);
	    
	    grabObj.appendChild(objDesign);
	    $(objDesign).addClass("btnsel");
	    
	curvyCorners(settings, ".btnsel");
    }
    var grabObj;
    var grabIndex;

    function createBTN(){
	
    for(var i=0; i<imageName.length; i++){ bindStuff(i);}
    
	    function bindStuff(i) {
		var newBTN;
		    newBTN = document.createElement("div");
		    newBTN.setAttribute('id', 'btn'+i);
		    newBTN.setAttribute('class', 'BtnStyleS');
		    
		    var grabBtnH = document.getElementById("btnH");
		    grabBtnH.appendChild(newBTN);
		    $(newBTN).addClass("BtnStyleS");
		    
		    creareText($(newBTN));
		
		$('#btn'+i).css({ 'opacity': .80 });
		$('#btn'+i).click(function(e){
		    
		    grabIndex = $(this).index()-1;
		    var gid = i;
			if (whatIMG != gid && isEnabled == true){
			    isEnabled = false;
			    whatIMG = gid;
			    grabObj = this;
			    createIMG();
			    create_onClick_Diplay();
			}
		});
	    }
	    
	//.add ronded edges.
	curvyCorners(settings, ".BtnStyleS");
	
	function creareText(newBTN){
	    var numberBTN = i+1;
	    $(newBTN).append('<t>'+numberBTN+'</t>');
	    var grabBtnH = document.getElementById('btnH');
	    disableSelection(grabBtnH) //.disable text selection on entire div of button holder div.
	}
	
	if ( whatBrowser.mozilla  ) {  //&& ua.version.slice(0,3) == "1.9"
	//alert( "Do stuff for firefox 3" );
	WebdingsSymbels = '<font face="Webdings">&#9658;</font>';
	}
	
	btnPlay();
    }
    
    var WebdingsSymbels = '<font face="Webdings">&#52;</font>'; //&#10132     // &0x34  //52   // 4   //9658
    
    function btnPlay(){
	    $('#btnPlay').remove();
		
		var newBTNP;
		newBTNP = document.createElement("div");
		newBTNP.setAttribute('id', 'btnPlay');
		newBTNP.setAttribute('class', 'BtnStyleS');
		$(newBTNP).html(WebdingsSymbels).text();
		
		var grabBtnH = document.getElementById("btnH");
		$(grabBtnH).prepend(newBTNP);
		$(newBTNP).addClass("BtnStyleS");
		
		$('#btnPlay').unbind('click');
		$("#btnPlay").css({ 'opacity': 0.8 });
		
		curvyCorners(settings, ".BtnStyleS"); // Corners	// BtnStyleS
		
		$(newBTNP).bind('click', btnPlayClick);
    }
    
    function btnPlayClick(){

	if (Playbtn == true){
	    Playbtn = false;
	    
	    WebdingsSymbels = '<font face="Webdings">;</font>';
	    
	    if ( whatBrowser.mozilla  ) {  //&& ua.version.slice(0,3) == "1.9"
		//alert( "Do stuff for firefox 3" );
		WebdingsSymbels = '<font face="Webdings">&#9646&#9646;</font>';
	    }
	    btnPlay();
	}
	else {
	WebdingsSymbels = '<font face="Webdings">4</font>';
	if ( whatBrowser.mozilla  ) {  //&& ua.version.slice(0,3) == "1.9"
	    //alert( "Do stuff for firefox 3" );
	    WebdingsSymbels = '<font face="Webdings">&#9658;</font>';  //9658
	}
	btnPlay();
	Playbtn = true;
	loopaniFun();
	}
    };
    
    
    
    
    
    
    
    
    
    function randomFirstIMG(){
        var numLow = 0;
        var numHigh = imageName.length-1;
        var adjustedHigh = (parseFloat(numHigh) - parseFloat(numLow));
        whatIMG = Math.floor(Math.random()*adjustedHigh) + parseFloat(numLow);
	
	grabIndex = whatIMG+1;
    }
    
    
    
    
    function addDropSahdowIMG(){
	//. dropShadowid
	
	var FimgS = new Image();
	var strS = folder+"Pan_dropshad"+FileType;
	
	$(FimgS).attr("src", strS + "?" + new Date().getTime());
	
	//$(FimgS).load(function() { //.not working fo some reason. !?
		//alert("hi");
		$('#dropShadowid').css({ 'background': 'url('+ FimgS.src +')' })
		.delay(1500)
		.animate({
			'opacity': '1'
			},
			{ duration:1000, easing:"quadEaseInOut"} );
	//});
	
    }
    
    
    function firstStuff(){
	
	randomFirstIMG();
	addDropSahdowIMG();
	
	createBTN();
	grabIndex--;
	grabObj = document.getElementById("btn"+grabIndex);
	create_onClick_Diplay();
	    
	    //. - Animate first image.
	    var firstloop = window.setTimeout(firstloopAni, 500, "JavaScript");	//. Animate first image.
	    function firstloopAni(){
		clearTimeout(firstloop);
		createIMG();
		
		    firstloop = window.setTimeout(aniInBtn, 2000, "JavaScript"); //. Animate first image.
		    function aniInBtn(){
			
			$('#btnH').animate({
			    //'opacity': '0.8',
			    'top': '192px'	   
					   },
			    { duration:500, easing:"quadEaseInOut"} );
			//$('#btnH').css({ 'visibility': 'visible' });
		    }
	    }
	    
    }
    
    
    
    var imageName = []; //.Name if images to be displayed using a xml file called images.xml
    var imageNameHead = [];
    var imageNameCom = [];
    var imageLinks = [];
    var totalImg = 0;   //.how many images.
    
loadXML();
    function loadXML(){
        $.ajax({
            type: "GET",
            url: xmlLocation, //varible at top.
            dataType : 'xml',
                success: function(xml) {
		    
                    $(xml).find('image').each(function(){
			    
			imageName[totalImg] = $(this).text(); //.Gets just the name of the image from the xml file.
			totalImg++;
                    });
		    totalImg = 0;
		    
                    $(xml).find('head').each(function(){
			imageNameHead[totalImg] = $(this).text();
			totalImg++;
                    });
		    totalImg = 0;
		    
                    $(xml).find('com').each(function(){
			imageNameCom[totalImg] = $(this).text();
			totalImg++;
                    });
		    totalImg = 0;
		    
                    $(xml).find('link').each(function(){
			imageLinks[totalImg] = $(this).text();
			totalImg++;
                    });
		    
		   firstStuff() 
		    
                }
	    ,error: function(jqxhr){
	     alert("error: loading pictures. Try refreshing your browser  or  browser not supported :( "); }
        })
    }
    
    
//}//.ie7crap 
});//.End jquery.
