﻿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 = 1000;
firstcheckCookie();
/* Cookies code starts */
function getCookie(c_name){
if (document.cookie.length>0){
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1){ 
    c_start=c_start + c_name.length+1 ;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return ""
}
function setCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toUTCString());
}
function checkCookie(){
var    lastimage= lastimg;
contshownew = contshow;
setCookie('lastimage',lastimage,365);
setCookie('contshownew',contshownew,365);
    //nextimg();
}
    
function firstcheckCookie(){
idnti=getCookie('lastimage');
//contshow = getCookie('contshownew');
    if (idnti!=null && idnti!="") {
    //    alert(idnti)
      }else{
         idnti=1 
      }
    //  alert(contshow)
}
/* Cookies code ends */
function contdisp(dispimg)  // Common function to display image on click ..This is the soul Function of code.  
     { 
         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);
//          });
  
             $('#content_'+lastimg).hide();
             $('#content_'+dispimg).show();
        lastimg=dispimg; 
          }
          checkCookie();
     } 
     function nextimg() // Function used for next image click
     { 
         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).hide();
             $('#content_'+1).show();
//            $('#content_'+lastimg).fadeOut(fadeTime/2, function(){
//                $('#content_'+1).fadeIn(fadeTime/2);
//            });
              lastimg = 1;
            }else if((id-1) != lastimg){  
                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);
             $('#content_'+lastimg).hide();
             $('#content_'+nextItem).show();

//                $('#content_'+lastimg).fadeOut(fadeTime/2, function(){
//                      $('#content_'+nextItem).fadeIn(fadeTime/2);
//                  });
                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).hide();
             $('#content_'+nextItem).show();
//            $('#content_'+lastimg).fadeOut(fadeTime/2, function(){
//                $('#content_'+nextItem).fadeIn(fadeTime/2);                                                    
//            });
              lastimg= (id-1);
        
        }else if( lastimg!= 1){  
            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);
             $('#content_'+lastimg).hide();
             $('#content_'+nextItem).show();
//            $('#content_'+lastimg).fadeOut(fadeTime/2, function(){
//                $('#content_'+nextItem).fadeIn(fadeTime/2);
//            });
              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/2, function(){
                $('#content_'+nextItem).fadeIn(fadeTime/2);
            });
            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: "sites.xml",
                dataType: "xml",
                success: function(xml) {
                    $(xml).find('SEARCH_RESULTS').each(function(){
                    //alert(1)
                     $(this).find('CLOB').each(function(){
                        $(this).find('ARTICLE').each(function(){
                            var conthead = $(this).find('HEADLINE').text();    
                            var contdetail = $(this).find('ABSTRACT').text();
                            var more = $(this).find('DISPLAY_URL').text();
                            var tmpContent = '<span class="yllow-cntnt">'+conthead+'</span><p>'+contdetail+'</p><span><a href="'+more+'">More</a></span>';
                            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('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 < 19)
                                    {
                                        //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('<img src="http://www.impressionsmag.com'+titlela+'" style="z-index:'+galid+';" />').appendTo('#slider1');
                                        $('<a class="select" href="javascript:contdisp('+id+')" id="thumb'+id+'">'+id+'</a>').html('<img src="images/blank.gif" />').appendTo('#paginate-slider1');
                                        lastimg=idnti;
                                        }else{
                                            //alert("gali")
                                        $('<div class="contentdiv" id="gal'+id+'" style="display:none;z-index:'+zindexvar+'"></div>').html('<img src="http://www.impressionsmag.com'+titlela+'" style="z-index:'+galid+';" />').appendTo('#slider1');
                                        $('<a class="noselect" href="javascript:contdisp('+id+')" id="thumb'+id+'">'+id+'</a>').html('<img src="images/blank.gif" />').appendTo('#paginate-slider1');
                                        }
                                        id=id+1;
                                    }
                                });
                            });
                        });/**/
                    });
                    });
                }
            });
        });
        
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 );
        }
}
