﻿var lastimg = 1;
var contshow = 1;
var id = 1;
var id2 = 1;
var galid=0;
var zindexvar=370;
var contentshow="";
var lastimage="";
var idnti=1;
var contshownew =1;
var fadeTime = 800;
var thumbLength = 0;
var lastImageSel =0;
var stepHeight =0;
var more;
var ctrl = 0;
var rand;
function showRandom(){
        rand = Math.floor(Math.random()*thumbLength);
        thumbScreenScroll = rand/4;
        mod = rand % 4;
        if(thumbScreenScroll >1){
            if(mod == 0){
                thumbScreenScroll = Math.floor(thumbScreenScroll)-1;
            }else{
                thumbScreenScroll = Math.floor(thumbScreenScroll);                            
            }
            var currentPosY = $('.pagination').css('top');
            var parentHeight = parseFloat($('.slider-parent').css('height'),10);
            var nextPosY = parseFloat(currentPosY, 10)-((parentHeight*thumbScreenScroll)+(3*thumbScreenScroll));
            $('.pagination').animate({top:nextPosY});
            document.getElementById("thumb"+rand).className='select';
            document.getElementById("thumb"+lastimg).className='noselect';
            var nextItem = parseInt(lastimg) +1;
            $('#gal'+lastimg).fadeOut(fadeTime);
            $('#gal'+rand).fadeIn(fadeTime);        
            //var cSlides=id-1;
            $('#content_'+lastimg).fadeOut(fadeTime/2, function(){
                $('#content_'+rand).fadeIn(fadeTime/2, function(){
                    ctrl = 0;
                    lastimg= rand;
                });
            });
        }else{
            document.getElementById("thumb"+rand).className='select';
            document.getElementById("thumb"+lastimg).className='noselect';
            var nextItem = parseInt(lastimg) +1;
            $('#gal'+lastimg).fadeOut(fadeTime);
            $('#gal'+rand).fadeIn(fadeTime);        
            var cSlides=id-1;
            $('#content_'+lastimg).fadeOut(fadeTime/2, function(){
                $('#content_'+rand).fadeIn(fadeTime/2, function(){
                    ctrl = 0;
                    lastimg= rand;
                });
            });
        }
    }
function contdisp(dispimg)  // Common function to display image on click ..This is the soul Function of code.  :)  Luv to code this
     { //alert(lastimg);
         if(ctrl==0){
            ctrl = 1;
            if(dispimg != lastimg){
              document.getElementById("thumb"+dispimg).className='select';
              document.getElementById("thumb"+lastimg).className='noselect';
              $('#gal'+lastimg).fadeOut(fadeTime);
              $('#gal'+dispimg).fadeIn(fadeTime);
              $('#content_'+lastimg).fadeOut(fadeTime/2, function(){            
                $('#content_'+dispimg).fadeIn(fadeTime/2, function(){
                    ctrl = 0;
                    lastimg=dispimg; 
                });
              });
             }
        }
          //checkCookie();
     } 
     function nextimg() // Function used for next image click
     {//alert(lastimg); 
         if(lastimg == (id-1)){ 
            /*document.getElementById("thumb"+(1)).className='select';
            document.getElementById("thumb"+lastimg).className='noselect';
            $('#gal'+lastimg).fadeOut(fadeTime);
            $('#gal'+1).fadeIn(fadeTime);
            $('#content_'+lastimg).fadeOut(fadeTime/2, function(){
                $('#content_'+1).fadeIn(fadeTime/2);
            });
              lastimg = 1;*/
            }else if((id-1) != lastimg){
                if(ctrl==0){
                    ctrl = 1;
                    lastImageSel = lastimg % 4;
                    if(lastImageSel == 0){
                        stepHeight = (51 * 4)+53;
                        var currentPosY = $('.pagination').css('top');
                        var nextPosY = parseFloat(currentPosY, 10)-stepHeight;
                        $('.pagination').animate({top:nextPosY});    
                    }
                    document.getElementById("thumb"+(parseInt(lastimg) + 1)).className='select';
                    document.getElementById("thumb"+lastimg).className='noselect';
                    var nextItem = parseInt(lastimg) +1;
                    $('#gal'+lastimg).fadeOut(fadeTime);
                    $('#gal'+nextItem).fadeIn(fadeTime);
                    //alert(cSlides);
                    //document.getElementById('content_'+lastimg).style.display = "none";
                    $('#content_'+lastimg).fadeOut(fadeTime/2, function(){
                        $('#content_'+nextItem).fadeIn(fadeTime/2, function(){
                            ctrl = 0;
                            lastimg= parseInt(lastimg) + 1;
                        });
                    });
                }
            }
            //checkCookie();
     }
 
    function runimg() // Function used for previous image click
    { 
        
         if(contshow ==1){  
             document.getElementById("runimg").className='runimg';
            contshow = 0;
         }else if(contshow ==0){
             document.getElementById("runimg").className='stopimg';
            nextimg();
            contshow = 2;
         }/*else if(contshow ==2){
             document.getElementById("runimg").className='runimg';
            contshow = 0;
         }*/
         //checkCookie();
    } 
    function previmg() // Function used for previous image click
    { 
        if(lastimg == 1){ 
            /*document.getElementById("thumb"+(id-1)).className='select';
            document.getElementById("thumb"+lastimg).className='noselect';
            var nextItem = id-1;
            $('#gal'+lastimg).fadeOut(fadeTime);
            $('#gal'+nextItem).fadeIn(fadeTime);
            $('#content_'+lastimg).fadeOut(fadeTime/2, function(){
                $('#content_'+nextItem).fadeIn(fadeTime/2);                                                    
            });
              lastimg= (id-1);*/
        }else if( lastimg!= 1){
            if(ctrl==0){
            ctrl = 1;
            lastImageSel = (lastimg-1) % 4;
            if(lastImageSel == 0){
                stepHeight = (51 * 4)+53;
                var currentPosY = $('.pagination').css('top');
                var nextPosY = parseFloat(currentPosY, 10)+stepHeight;
                $('.pagination').animate({top:nextPosY});    
            }
            document.getElementById("thumb"+(lastimg - 1)).className='select';
            document.getElementById("thumb"+lastimg).className='noselect';
            var nextItem = lastimg-1;
            $('#gal'+lastimg).fadeOut(fadeTime);
            $('#gal'+nextItem).fadeIn(fadeTime);
            //document.getElementById('content_'+lastimg).style.display = "none";
            $('#content_'+lastimg).fadeOut(fadeTime/2, function(){
                $('#content_'+nextItem).fadeIn(fadeTime/2, function(){
                    ctrl = 0;
                    lastimg= lastimg - 1;
                });
            });
            }
        }
        //checkCookie();
    }
    function contdisp1()  // THis function is right now Not use ful.. But can be used to go last and first images
    { 
        if((id-1)!= lastimg){  
            document.getElementById("thumb"+(id-1)).className='select';
            document.getElementById("thumb"+lastimg).className='noselect';
            var nextItem = id-1;
            $('#gal'+lastimg).fadeOut(fadeTime);
            $('#gal'+nextItem).fadeIn(fadeTime);
            $('#content_'+lastimg).fadeOut(fadeTime/4, function(){
                $('#content_'+nextItem).fadeIn(fadeTime/4);
            });
            lastimg=id-1;
        }if(lastimg == (id-1)){
        //    document.getElementById("lastimg").className='lastsel';
            //document.getElementById("firstimg").className='firstimg';
        }    
        //checkCookie();
    }
         $(document).ready(function(){
            $.ajax({
                type: "GET",
                url: "/contract/inc/flash/homepage_flash.xml",
                dataType: "xml",
                success: function(xml) {
                    $(xml).find('SEARCH_RESULTS').each(function(){                                                                
                        thumbLength =  $(xml).find('CLOB').length;
                        $(this).find('CLOB').each(function(){
                        $(this).find('ARTICLE_EXTRA').each(function(){
                        $(this).find('TEMPLATE_SPEC').each(function(){ 
                                   $(this).find('ADDITIONAL_INFORMATION').each(function(){
                                    more= $(this).find('DISPLAY_URL').text();
                                    
                            });});});                                   
                                                           
                        $(this).find('ARTICLE').each(function(){
                            var conthead = $(this).find('HEADLINE').text();    
                            var contdetail = $(this).find('ABSTRACT').text();
                            //var more = $(this).find('DISPLAY_URL').text();
                            //alert(more);
                            var tmpContent = '<div class="contentheadline"><a href="'+more+'"><span class="yllow-cntnt">'+conthead+'</span></a></div><div class="contentabstract"><p>'+contdetail+'</p></div><div class="slide-no">Slide '+id+' of '+thumbLength+'</div>';
                            if(id2==idnti){    
                            
                                $('<span id="content_'+id+'" style="display:block;" class="textContent"></span>').html(tmpContent).appendTo('.outercont');
                                id2++;        
                                lastimg=idnti;
                            }else{
                                $('<span id="content_'+id+'" style="display:none;" class="textContent"></span>').html(tmpContent).appendTo('.outercont');
                                id2++;
                            }
                        });
                        $(this).find('ARTICLE_EXTRA').each(function(){
                        //alert(3)
                            $(this).find('TEMPLATE_SPEC').each(function(){ 
                                   $(this).find('ADDITIONAL_INFORMATION').each(function(){
                                    more= $(this).find('DISPLAY_URL').text();
                                    
                            });});
                            $(this).find('PHOTOS').each(function(){ 
                            //alert(4)
                               $(this).find('PHOTO').each(function(){
                                    //alert(5)
                                    var title = $(this).find('SMALL_PHOTO_LINK').text();
                                    var titlela = $(this).find('LARGE_PHOTO_LINK').text();
                                    if(id < 60)
                                    {
                                        //alert(idnti)
                                        galid=galid+id;
                                        zindexvar= zindexvar+id; 
                                        //alert(galid+"=="+idnti)
                                        if(id==idnti){
                                    //alert("galid==idnti")
                                        $('<div class="contentdiv" id="gal'+id+'" style="display:block;z-index:'+zindexvar+'"></div>').html('<a href="'+more+'"><img src="'+titlela+'" style="z-index:'+galid+';" /></a>').appendTo('#slider1');
                                        $('<a class="select" href="javascript:contdisp('+id+')" id="thumb'+id+'">'+id+'</a>').html('<img src="'+title+'" style="z-index:'+galid+'; width:100px; height:50px;" />').appendTo('#paginate-slider1');
                                        lastimg=idnti;
                                        }else{
                                            //alert("gali")
                                        $('<div class="contentdiv" id="gal'+id+'" style="display:none;z-index:'+zindexvar+'"></div>').html('<a href="'+more+'"><img src="'+titlela+'" style="z-index:'+galid+';" /></a>').appendTo('#slider1');
                                        $('<a class="noselect" href="javascript:contdisp('+id+')" id="thumb'+id+'">'+id+'</a>').html('<img src="'+title+'" style="z-index:'+galid+'; width:100px; height:50px;" />').appendTo('#paginate-slider1');
                                        }
                                        id=id+1;
                                    }
                                });
                            });
                        });/**/
                    });
                        showRandom();
                    });
                }
            });
        });
        
function defrun()
{ 
    if(contshow==1)
    {
            //setInterval( "defrun1()", 5000 );
    }
    
    /*else if(contshow==2)
    {       
            contshow=1; 
            setInterval( "defrun1()", 0 );
    }*/
    else
    {}
}
function defrun1()
{  //alert(contshow)
        if(contshow!=0)
        {
               //setTimeout("nextimg()",100 );
        }
}
