﻿var mycarousel_itemList;
var catSelected = "";
var firstPass = true;
var carouselObject;

function PageMethod(fn, paramArray, successFn, errorFn)   
{   
  var pagePath = window.location.pathname;   
  //Create list of parameters in the form:   
  //{"paramName1":"paramValue1","paramName2":"paramValue2"}   
  var paramList = '';   
  if (paramArray.length > 0)   
  {   
	for (var i=0; i<paramArray.length; i+=2)   
	{   
	  if (paramList.length > 0) paramList += ',';   
	  paramList += '"' + paramArray[i] + '":"' + paramArray[i+1] + '"';    
	}   
  }   
  paramList = '{' + paramList + '}';   
  //Call the page method   
  jQuery.ajax(
  {   
	  type: "POST",   
	  url: pagePath + "/" + fn,   
	  contentType: "application/json; charset=utf-8",   
	  data: paramList,   
	  dataType: "json",   
	  success: successFn,   
	  error: errorFn   
  })   
;}   

function OnRequestComplete(result) 
{
	mycarousel_itemList =  eval('(' + result + ')');
	
	if(firstPass)
	{
		jQuery('#mycarousel').jcarousel(
		{
			scroll: 4,
			initCallback: mycarousel_initCallback,
			itemLoadCallback: mycarousel_itemLoadCallback
		});	
		firstPass = false;
	}	
	else
	{
		mycarousel_reset();
	}
}

function OnRequestError(result) 
{
	if (result != null) 
	{
		//alert(result);
	}
}

function mycarousel_reset()
{
    carouselObject.reset();
};

function SubmitData() 
{
	var cat = "";
	if(document.getElementById('downloadFilter')!=null) cat = document.getElementById('downloadFilter').value;
	PageMethod("GetData", ["cat", cat], OnRequestComplete, OnRequestError); //With parameters
}

/*-----------------------*/

function mycarousel_initCallback(carousel, state)
{
    carouselObject = carousel;
    if (state == 'reset') return;
    
    jQuery('#downloadFilter').change(function(e)
	{
		SubmitData();
    });
};

function mycarousel_itemLoadCallback(carousel, state)
{
    for (var i = carousel.first; i <= carousel.last; i++) 
    {
        if (carousel.has(i)) 
        {
            continue;
        }

        if (i > mycarousel_itemList.length) 
        {
            break;
        }
        carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[i-1]));
    }
    carousel.size(mycarousel_itemList.length);
};

function positionRatings(productRating)
{
	switch(productRating)
	{
		case "5":
			return "60px";
		case "4":
			return "50px";
		case "3":
			return "40px";
		case "2":
			return "30px";
		case "1":
			return "20px";
		case "0":
			return "10px";
		default:
			return "10px";
	}
}


function mycarousel_getItemHTML(item)
{
    var iconHtml = '<a href="' + item.productURL + '" class="downloadItems"><span>' + item.downloadType + '</span><br/>';
    iconHtml += '<img src="' + item.url + '" width="75" height="75" alt="' + item.title + '" /><br/>';
    var currentURL = location.href.toLowerCase();
    if(currentURL.indexOf("promotions") > -1)
    {
		iconHtml +=  '<div class="getItNow">Get It Now</div>';
		iconHtml +=  item.title + '<br/>';
		iconHtml +=  '$' + item.price;
    }
    else
    {
		iconHtml +=  item.title + '<br/>';
		iconHtml +=  '$' + item.price;
		if(item.downloadType != "ringtone")
		{
			iconHtml +=  '<div class="productRatings" style="background-position: 0px ' + positionRatings(item.rating) + ';"></div>';
		}
    }
    
    iconHtml += '</a>';
    return iconHtml;
};

jQuery(document).ready(function() 
{
	SubmitData();
});