
jQuery(function($){ 
    
    /* if a cookie is present, the set the browserGUID to it's value,
    otherwise set it to the user_guid value that is set in the header.inc 
    that is generated via the PHP session */
    var browserGUID = readCookie( "browserGUID" );
    if ( browserGUID == null || browserGUID == "" )
    {
    	browserGUID = user_guid;
    	createCookie( "browserGUID", browserGUID, 1000 );  //save guid for 1000 days
    } 
    
    /* on every page load lets make sure the PHP session_id is set to the proper value,
    so the browser, and the server are always in snyc */
    $.ajax({
        type: "GET",
        url: 'ajax/set_session.php',
        data: {user_guid: browserGUID}
    }); 
    
    
    $('input[@type=radio].star').rating({split: 2});
    
   //$.fn.fancyzoom.defaultsOptions.imgDir='images/zoom/';
   // $('a.zoom').fancyzoom({Speed:500});
   
   setupZoom(); 
    
    $('a.admin').click(function(){
        if ( !confirm("Are you sure you want to do this?") ) 
        {
            return false;
        }
    });

    $('#browseScroller').scroll(function(){
        var offset = $('#browseScroller').scrollTop();
        $('#capture_offset').val(offset);
    }); 
    
    $('div.star_live a').click(function(){
        var submission_id = $(this).parent().parent('form').find("input[@name='submission_id']").val(); 
        var rating = $(this).html(); 
        var user_guid = browserGUID;  

        $.ajax({
            type: "GET",
            url: 'ajax/rate.php',
            data: {submission_id: submission_id, rating: rating, user_guid: user_guid},
            success: function(data){ 
               if(data) {
                   $('div.submission div.overallRating div').html(data);  
               }                                                        
            }   
        });
        
        $.ajax({
            type: "GET",
            url: 'ajax/user_rating.php',
            data: {submission_id: submission_id},
            success: function(data){  
                   // $('div.submission div.userRating').find("input[@name='rating']").val(rating);
                   //                  //$('div.submission div.userRating div.star_live').removeClass('star_on');
                   //                  $('div.submission div.userRating div.star_live a').each(function(i){
                   //                      var num = parseFloat( $(this).html() );
                   //                      console.log(rating + ' = ' + num); 
                   //                      $(this).parent().removeClass('star_on');
                   //                      if( rating >= num )
                   //                      {
                   //                          $(this).parent().addClass('star_on');
                   //                      }                                            
                   //                  }); 
                   
                $('div.submission div.userRating div').html(data);
                //$('input[@type=radio].star').rating({split: 2}); 
		          
		                             
            } 
        }); 
        
        return false;  
    });
    
    $('#browse').submit(function(){  
        var url = $('#browse_url').val(); 
        if(url == '' || url == 'http://')
        {
            $('#errors').html('Please enter a valid URL');
            return false;
        }   
    });
    
    // Stripe table rows
	$('.tourDates tr').mouseover(function(){
		$(this).addClass('tableRowHover');
	}).mouseout(function(){
		$(this).removeClass('tableRowHover');
	});
	$('.tourDates tr:even').addClass('altRow');
    
});


















function parseQueryString (str) {
  str = str ? str : location.search;
  var query = str.charAt(0) == '?' ? str.substring(1) : str;
  var args = new Object();
  if (query) {
    var fields = query.split('&');
    for (var f = 0; f < fields.length; f++) {
      var field = fields[f].split('=');
      args[unescape(field[0].replace(/\+/g, ' '))] = unescape(field[1].replace(/\+/g, ' '));
    }
  }
  return args;
}

function generateGuid()
{
	var result, i, j;
	result = '';
	for(j=0; j<32; j++)
	{
		if( j == 8 || j == 12|| j == 16|| j == 20)
			result = result + '-';
		i = Math.floor(Math.random()*16).toString(16).toUpperCase();
		result = result + i;
	}
	return result;
}  

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
} 
